package test

import cn.hutool.log.Log
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.actor
import kotlinx.coroutines.channels.consumeEach

private val log by lazy { Log.get() }

@OptIn(ObsoleteCoroutinesApi::class)
suspend fun main() {
  coroutineScope {
    val test =
        async(start = CoroutineStart.LAZY) {
          log.info("async calc")
          delay(1000)
          "test"
        }
    val testActor = actor {
      channel.consumeEach { command ->
        runCatching { handleCommand(command) }
            .onSuccess { log.info("{}", it) }
            .onFailure { log.error(it) }
      }
    }
    testActor.send(test.await())
    testActor.send(test.await())

    // testActor.close()
  }

  println("success...")
}

private suspend fun handleCommand(message: String) {
  when (message) {
    "test" -> log.info("test")
    "test1" -> log.info("test1")
    else -> TODO()
  }
}
